<?php

class AjaxR
{
    /**
     *
     * @var type bool 操作是否成功
     */
    public $success = false;
    
    /**
     *
     * @var string  message to page 
     */
    public $msg = "";
    
    /**
     *
     * @var string if need  the key  
     */
    public $id = "";
    
    /**
     *
     * @var array data
     */
    public $data = array();
    
    /**
     *  动作名称  用于操作日志入库
     * @var string 
     */
    public $action = "";

	/**
	*	操作模块名称  用于操作日志入库
	*	@var string
	*/
	public $model = "";
    
    /**
     * 输出对象，结束
     */
    public function flush($exit = true)
    {
		
        if (!property_exists($this, "noLog")){

            $this->_write();
        }

		if($exit){
        	echo json_encode($this);exit;
        }
    }

	private function _write()
	{
		$result = $this->success == 1 ? "操作成功" : "操作失败 -> " .  $this->msg;
		$this->WriteLog($this->model, $this->action, $result);
	}
	
	
    /**
	*  写入系统操作日志。
	* @param type $model
	* @param type $action
	* @param type $result 
	*/
    public function WriteLog($model, $action, $result)
    {
        $lm = new SystemLog();
        $lm->IP = $_SERVER["REMOTE_ADDR"];
        $lm->Time = date("Y-m-d H;i:s");
        $lm->User = Yii::app()->user->name;
        $lm->Model = $model;
        $lm->Action = $action;
        $lm->Result = $result;
        $r = $lm->save();
        return $r;
        //var_dump($lm->getErrors());
    }
}

?>
